﻿using SuporeSliplampApp.Base;
using SuporeSliplampApp.Control;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

namespace SuporeSliplampApp.Util
{
    public class Common : NotifyBase
    {
        public static readonly string basePath = System.AppDomain.CurrentDomain.BaseDirectory;

        public static string comPath = basePath + "Upload";

        public static System.Windows.Point picpostion = new System.Windows.Point();

        public static bool isexpo = false;

        public static bool iswhite = false;

        public static bool isAdminMode = false;

        public static string languageCode = "zh-CN";

        public static bool isAddToNow = false;

        public static List<ScrollControl> scrolllist;

        public static int DateIndex = 0;

        //public static bool isIDSwitchOn = false;

        //public static string patid;

        //public static int patidLength = 1;
        public static bool ExsistPath()
        {            
            try
            {
                if (!Directory.Exists(comPath))
                {
                    Directory.CreateDirectory(comPath);
                }
                    
                return true;
            }
            catch (Exception)
            {
                return false;               
            }           
        }

        public static bool savePic(string path,string newName)
        {
            //BitmapImage bitmapImage = new BitmapImage(new Uri(path));
            //string format = bitmapImage.Format.ToString();
            string extension = Path.GetExtension(path);
            extension = extension.Replace(".","");
            if (!ExsistPath())return false;
            if (saveJpg(path,newName))
                return true;
            else
                return false;
        }

        private static bool saveJpg(string path,string newName)
        {
            try
            {
                BitmapImage originalImage = new BitmapImage();
                originalImage.BeginInit();
                originalImage.UriSource = new Uri(path);
                originalImage.EndInit();

                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(originalImage));
                using (FileStream fileStream = new FileStream((comPath +@"\"+ newName), FileMode.Create))
                {
                    encoder.Save(fileStream);
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
                
            }
            
        }

        private static bool savePng(string path, string newName, string newPath)
        {
            try
            {
                BitmapImage originalImage = new BitmapImage();
                originalImage.BeginInit();
                originalImage.UriSource = new Uri(path);
                originalImage.EndInit();

                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(originalImage));
                using (FileStream fileStream = new FileStream((newPath + newName), FileMode.Create))
                {
                    encoder.Save(fileStream);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            
        }
        public class ViewClass
        {
            public const string MAIN = "SuporeSliplampApp.Views.MainContent";
            public const string CAMERA = "SuporeSliplampApp.Views.CamaraView";
            public const string PATIENT = "\"SuporeSliplampApp.Views.MainContent\"";            
        }

    }
}
